export const currencyWFormatter = (value: number, dot: number = 2, unit: string = '万'): string => {
  value = value || 0;
  if (value >= 10000 || value <= -10000) {
    return (value / 10000).toFixed(dot) + unit;
  } else {
    return Number(value) === 0 ? '0' : value.toFixed(dot);
  }
};
export const currencyWCFormatter = (value: number, dot: number = 2, unit: string = '万'): string => {
  value = value || 0;
  if (value >= 10000 || value <= -10000) {
    return Number((value / 10000).toFixed(dot)).toLocaleString() + unit;
  }
  return Number(value.toFixed(dot)).toLocaleString();
};
export const currencyWNFormatter = (value: number, dot: number = 0): string => {
  return Number((value / 10000).toFixed(dot)).toLocaleString();
};
export const currencyWNDFormatter = (value: number, dot: number = 0): number => {
  return Number((value / 10000).toFixed(dot));
};
export const percent2DFormatter = (value: number, dot: number = 2): string => {
  return value ? `${(value * 100).toFixed(dot)}%` : '0%';
};
export const diffPercent2Formatter = (thisData: number, lastData: number, dot: number = 2): string => {
  const rate = growthRate1(thisData, lastData);
  return `${(rate * 100).toFixed(dot)}%`;
};
export const growthRate1 = (current: number, previous: number): number => {
  if (previous === 0) {
    return current === 0 ? 0 : 1; // 如果去年是0，当前是0则增长率为0，否则视为100%
  }
  return (current - previous) / previous;
};

export const growthRate = (thisData: number, lastData: number): number => {
  return (thisData - lastData) / Math.abs(lastData);
};

export const completRate = (thisData: number, budgetData: number): number => {
  return growthRate(thisData, budgetData) + 1;
};
export const comPercent2Formatter = (thisData: number, budgetData: number, dot: number = 2): string => {
  const g = (growthRate(thisData, budgetData) + 1) * 100;
  return `${g.toFixed(dot)}%`;
};
export const diff = (thisData: number, lastData: number): number => {
  if (lastData === 0 && thisData === 0) {
    return 0; // 增长率为 0
  }
  if (lastData === 0) {
    return thisData > 0 ? 0 : -0; // 返回正无穷或负无穷
  }
  const growthRate = (thisData - lastData) / Math.abs(lastData);
  return growthRate;
};
export const dot2Formatter = (value: number, dot: number = 2): string => {
  value = value || 0;
  return Number(value.toFixed(dot)).toLocaleString();
};
export const dot2FormatterRemoveComma = (value: number | string, dot: number = 2): string => {
  const num =
    typeof value === 'string'
      ? Number(value.replace(/,/g, '')) // 移除所有逗号
      : Number(value) || 0;
  return num.toFixed(dot).toLocaleString();
};
export const safeDivide = (numerator: number, denominator: number): number => {
  return denominator === 0 || !numerator || !denominator ? 0 : numerator / denominator;
};
export const currencyWIFormatter = (value: number, dot: number = 2, unit: string = '万'): string => {
  value = value || 0;
  if (value >= 10000 || value <= -10000) {
    return Number((value / 10000).toFixed(dot)).toLocaleString() + unit;
  }
  return Number(value.toFixed(0)).toLocaleString();
};

export const maxAbsValue = (arr: number[][]): number => {
  return arr.flat().reduce((max: number, num: number) => {
    const abs = Math.abs(num);
    return abs > max ? abs : max;
  }, 0);
};

export const calculateRadius = (currentValue: number, maxValue: number, minRadius: number = 2, maxRadius: number = 10): number => {
  return (maxRadius - minRadius) * (currentValue / maxValue) + minRadius;
};
